<template>
<div id="train">
  <div id="trainAuth">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/train' }">干部培训</el-breadcrumb-item>
      <el-breadcrumb-item>授权</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
      <!-- <div id="editswitch" v-if="isAdmin" style="float:left"><el-switch v-model="editVisible" active-color="#13ce66" inactive-color="#ff4949" :change="editBtnsShow()"></el-switch> <span>编辑</span></div> -->
      <el-button v-show="editBtnsVisible" type="success" @click="handleAdd" icon="el-icon-plus" style="margin:10px">新增</el-button>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <el-table :data="authList" highlight-current-row v-loading="listLoading" style="width: 100%;" :height="tableHeight" border>
        <el-table-column type="index" label="序号" width="70" align="center">
        </el-table-column>
        <el-table-column prop="realName" label="授权用户">
        </el-table-column>
        <!-- <el-table-column prop="auth" label="权限" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.auth==='1'" style="color:green"><b>查看</b></span>
            <span v-else style="color:blue"><b>管理</b></span>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <!-- <el-button size="small" @click="handleEdit(scope.row)" type="primary" style="margin:10px 0">修改</el-button> -->
            <el-button type="danger" size="small" @click="handleDel(scope.row.id)" style="margin:10px 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div>
      <!--新增界面-->
      <el-dialog title="新增授权" :visible.sync="addFormVisible">
        <el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
          <el-form-item label="用户" prop="sysUserId">
            <div v-if="sysUserId">您选择了用户：<b>{{realName}}</b></div>
            <div v-else>请选择用户</div>
            <el-tree style="clear:both" accordion node-key="index" @current-change="testf" :expand-on-click-node="false" :render-content="renderContent" class="filter-tree" :data="treedata" ref="tree2" empty-text="正在加载..."  :props="myprops"></el-tree>
          </el-form-item>
          <!-- <el-form-item label="权限" prop="auth">
            <el-radio-group v-model="addForm.auth">
              <el-radio v-for="(item,index) in auths" :key="'auth'+index" :label="index+1">{{item}}</el-radio>
            </el-radio-group>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="addFormSubmit" :loading="addLoading">提交</el-button>
        </div>
      </el-dialog>
      <!--修改界面-->
      <el-dialog title="修改授权" :visible.sync="editFormVisible">
        <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
          <el-form-item label="用户" prop="sysUserId">{{realName}}
          </el-form-item>
          <!-- <el-form-item label="权限" prop="auth">
            <el-radio-group v-model="editForm.auth">
              <el-radio v-for="(item,index) in auths" :key="'auth'+index" :label="index+1">{{item}}</el-radio>
            </el-radio-group>
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="editFormVisible = false">取消</el-button>
          <el-button type="primary" @click.native="editFormSubmit" :loading="editLoading">提交</el-button>
        </div>
      </el-dialog>
    </section>
  </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      changeSizeFlag: false,
      total: 0,
      pageNum: 1,
      pageSize: 20,
      realName: '',
      auths: ['查看', '管理'],
      sysUserId: null,
      authList: [],
      myprops: {
        isLeaf: 'isLeaf'
      },
      TreeLoading: false,
      treedata: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      tableHeight: 100,
      maxId: 0,
      isAdmin: true, // 临时调整为true
      isChuzhang: false,
      editVisible: false,
      editBtnsVisible: true,
      shhlist: [],
      listLoading: false,
      totalnum: 0,
      addFormVisible: false, // 新增界面是否显示
      addLoading: false,
      // 新增界面数据
      addForm: {
        auth: ''
        // type1: false,
        // type2: false,
        // type3: false
      },
      addFormRules: {
      },
      editFormRules: {
      },
      editFormVisible: false, // 编辑界面是否显示
      editLoading: false,
      // 编辑界面数据
      editForm: {
        auth: '',
        // type1: false,
        // type2: false,
        // type3: false,
        id: 0
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getList()
      }
    },
    testf () {
      if (this.$refs.tree2.getCurrentNode()['id'] !== '0') {
        this.sysUserId = this.$refs.tree2.getCurrentNode()['id']
        this.realName = this.$refs.tree2.getCurrentNode()['label']
      }
      console.log('this.sysUserId:' + this.sysUserId)
    },
    getTree () {
      this.TreeLoading = true
      this.$http
        .get(this.$http.defaults.trainingAPI + `api/trainingBusAuth/getAllUsers`)
        .then(response => {
          this.TreeLoading = false
          console.log('获取成功')
          console.log(response.data)
          this.treedata = response.data.result
          // this.treedata = response.data.filter(item => item.label !== '其他单位')
          // this.treedata.forEach(item => {
          //   if (item.children && item.children.length > 0) {
          //     item.children.map(it => {
          //       it.label = it.label + 'xxx111'
          //     })
          //   }
          // })
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    renderContent (h, { node, data, store }) {
      console.log('测试：' + this.value1)
      return (
        <span class="custom-tree-node" style="width:100%" title={data.title} >
          <i class={data.className} />
          <span style="line-height:28px">{node.label}</span>
        </span>
      )
    },
    showperson (id) {
      this.mypersonid = id
      console.log('mypersonid:' + this.mypersonid)
      this.showpersonVisible = true
    },
    editBtnsShow () {
      console.log(this.editVisible)
      this.editBtnsVisible = this.editVisible
    },
    // 获取用户列表
    getList () {
      this.listLoading = true
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      let para = {
        'pageNum': this.pageNum,
        'pageSize': this.pageSize
      }
      this.$http
        .post(this.$http.defaults.trainingAPI + `api/trainingBusAuth/queryByPage`, para)
        .then(response => {
          this.listLoading = false
          if (response.data.success === true) {
            console.log('获取成功')
            this.authList = response.data.result.records
            this.totalnum = response.data.result.total
            this.changeSizeFlag = false
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch((error) => {
          console.log(error)
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    // 显示新增界面
    handleAdd: function () {
      this.addFormVisible = true
      console.log('显示新增界面:' + this.addFormVisible)
      this.realName = null
      this.sysUserId = null
      // this.$set(this.addForm, 'type1', false)
      // this.$set(this.addForm, 'type2', false)
      // this.$set(this.addForm, 'type3', false)
    },
    // 显示编辑界面
    handleEdit (data) {
      if (data) {
        let objData = Object.assign({}, data)
        console.log(objData)
        this.sysUserId = objData.sysUserId
        this.realName = objData.realName
        this.editForm.id = objData.id
        this.$set(this.editForm, 'auth', parseInt(objData.auth))
        // this.$set(this.editForm, 'type1', !!objData.type1)
        // this.$set(this.editForm, 'type2', !!objData.type2)
        // this.$set(this.editForm, 'type3', !!objData.type3)
        this.editFormVisible = true
      }
    },
    // 新增
    addFormSubmit () {
      if (!this.sysUserId) {
        this.$message({
          message: '请选择授权用户',
          type: 'error'
        })
        return false
      }
      // if (!this.addForm.type1 && !this.addForm.type2 && !this.addForm.type3) {
      //   this.$message({
      //     message: '请至少选择一支队伍',
      //     type: 'error'
      //   })
      //   return false
      // }
      // if (!this.addForm.auth) {
      //   this.$message({
      //     message: '请选择权限',
      //     type: 'error'
      //   })
      //   return false
      // }
      this.editLoading = true
      let para = {
        // type1: this.addForm.type1 ? 1 : 0,
        // type2: this.addForm.type2 ? 1 : 0,
        // type3: this.addForm.type3 ? 1 : 0,
        // auth: this.addForm.auth,
        auth: 1,
        sysUserId: this.sysUserId,
        realName: this.realName
      }
      this.$http.post(this.$http.defaults.trainingAPI + `api/trainingBusAuth/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '提交成功',
            type: 'success'
          })
          this.$refs['addForm'].resetFields()
          this.sysUserId = null
          this.realName = null
          // this.addForm.type1 = 0
          // this.addForm.type2 = 0
          // this.addForm.type3 = 0
          this.addFormVisible = false
          this.editLoading = false
          this.getList()
        } else {
          console.log('提交失败:' + response.data.message)
          this.$message({
            message: '提交失败:' + response.data.message,
            type: 'error'
          })
          this.editLoading = false
        }
      }).catch((error) => {
        console.log(error)
        this.$message({
          message: '提交失败:' + error,
          type: 'error'
        })
        this.editLoading = false
      })
    },
    // 编辑
    editFormSubmit: function () {
      // if (!this.editForm.type1 && !this.editForm.type2 && !this.editForm.type3) {
      //   this.$message({
      //     message: '请至少选择一支队伍',
      //     type: 'error'
      //   })
      //   return false
      // }
      if (!this.editForm.auth) {
        this.$message({
          message: '请选择权限',
          type: 'error'
        })
        return false
      }
      this.editLoading = true
      let para = {
        id: this.editForm.id,
        // type1: this.editForm.type1 ? 1 : 0,
        // type2: this.editForm.type2 ? 1 : 0,
        // type3: this.editForm.type3 ? 1 : 0,
        auth: this.editForm.auth,
        sysUserId: this.sysUserId,
        realName: this.realName
      }
      this.$http.post(this.$http.defaults.trainingAPI + `api/trainingBusAuth/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '提交成功',
            type: 'success'
          })
          this.$refs['editForm'].resetFields()
          this.editFormVisible = false
          this.editLoading = false
          this.getList()
        } else {
          console.log('提交失败:' + response.data.message)
          this.$message({
            message: '提交失败:' + response.data.message,
            type: 'error'
          })
          this.editLoading = false
        }
      }).catch((error) => {
        console.log(error)
        this.$message({
          message: '提交失败:' + error,
          type: 'error'
        })
        this.editLoading = false
      })
    },
    // 删除
    handleDel: function (id) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        let para = { ids: [id] }
        this.$http.post(this.$http.defaults.trainingAPI + `api/trainingBusAuth/remove`, para).then(response => {
          if (response.data.success === true) {
            this.addLoading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
      })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 272
      })
    }
  },
  mounted () {
    this.getTree()
    this.getList()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
  #train .el-table .el-table__cell{
    padding: 12px 0;
  }
  #train .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  #train .wboxd .el-table td,
  #train .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #train .el-table--border th,
  #train .el-table--border td {
    border-right: 1px solid #95B8E7;
  }
  #train .el-table th.is-leaf,
  #train .el-table td {
    border-bottom: 1px solid #95B8E7;
  }
  #train .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }

</style>
<style scoped>
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #trainAuth .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  #trainAuth .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
